package com.esunrising.dmtest.controller;

import com.esunrising.dmtest.dto.Result;
import com.esunrising.dmtest.entity.TestTab;
import com.esunrising.dmtest.service.TestTabService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/demo")
@Slf4j
public class TestTabController {
    @Resource
    private TestTabService testTabService;


    @GetMapping("/findAll")
    public Result<List<TestTab>> findAll(){
        List<TestTab> all = testTabService.getAll();
        return Result.ok(all);
    }

    @GetMapping("/findById")
    public Result<TestTab> findById(Integer id){
        TestTab byId = testTabService.getById(id);
        return Result.ok(byId);
    }

    @PostMapping("/save")
    public Result<Object> save(@RequestBody TestTab testTab){
        System.out.println(testTab.getID());
        System.out.println(testTab.getNAME());
        TestTab ret = testTabService.save(testTab);
        if (ret != null && ret.getID() != null){
            return Result.ok(ret);
        }else{
            return Result.ok(500,"保存失败", null);
        }
    }

    @DeleteMapping("/delete")
    public Result<Void> delete(Integer id){
        try {
            testTabService.delete(id);
            return Result.ok();
        } catch (Exception e) {
//            throw new RuntimeException(e);
            return Result.error("删除失败,参考错误："+e.getMessage());
        }
    }
}
